#pragma once
////#include "stdafx.h"
#include "Link.h"
namespace org
{
	namespace spark
	{
		namespace core
		{

			Link::Link()
			{
			}

			Link::Link(Agent *end1, Agent *end2)
			{
				this->end1 = end1;
				this->end2 = end2;

				if (end1 != 0)
					end1->addLink(this);

				if (end2 != 0)
					end2->addLink(this);
			}

			void Link::removeEnd(Agent *end)
			{
				if (end == 0)
					return;

				if (end1 == end)
// WARNING:  converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code:
					delete end1;

				if (end2 == end)
// WARNING:  converted the original 'null' assignment to a call to 'delete', but you should review memory allocation of all pointer variables in the converted code:
					delete end2;
			}

			Agent *Link::getEnd1()
			{
				return end1;
			}

			Agent *Link::getEnd2()
			{
				return end2;
			}

			void Link::connect(Agent *end1, Agent *end2)
			{
				if (this->end1 != 0)
					this->end1->removeLink(this);

				if (this->end2 != 0)
					this->end2->removeLink(this);

				this->end1 = end1;
				this->end2 = end2;

				if (end1 != 0)
					end1->addLink(this);

				if (end2 != 0)
					end2->addLink(this);
			}

			bool Link::isConnectedTo(Agent *end)
			{
				if (end == 0)
					return false;

				return end1 == end || end2 == end;
			}

			void Link::die()
			{
				if (end1 != 0)
					end1->removeLink(this);

				if (end2 != 0)
					end2->removeLink(this);

				Agent::die();
			}
		}
	}
}